package com.cogent.report;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.cogent.common.io.FileUtil;
import com.cogent.common.util.Globals;

public class ReportStream {
	private static String repdir =  Globals.getRootPath() + "selfreportset";
	public void writeStream(String fileName, ReportSetting rset) throws ClassNotFoundException, IOException{
		FileUtil.createDir(repdir);
		ObjectOutputStream out = null;
		try{
			out =
				new ObjectOutputStream(
						new FileOutputStream(repdir + File.separator + fileName + ".out"));
			out.writeObject(rset);	
		}finally{
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}
	
	public ReportSetting readStream(String fileName) throws ClassNotFoundException, IOException{
		ReportSetting rset = new ReportSetting();
		ObjectInputStream in = null;
		try{
			in = new ObjectInputStream(
					new FileInputStream(repdir + File.separator + fileName + ".out")); 
			rset = (ReportSetting)in.readObject();
		}finally{
			if(in != null){
				in.close();
			}
		}
		
		return rset;
	}
	
	public boolean isFileExist(String fileName){
		File file = new File(repdir + File.separator + fileName + ".out");
		
		return file.exists();
	}

}
